<template>
<div id="root">
  <Top><p>选择地址</p></Top>
  <van-field
      required v-model="name" center clearable size="10px" placeholder="请填写你的姓名">
  </van-field>
  <van-field
      required v-model="school" center clearable size="10px" @focus="addsite()" placeholder="小区/写字楼/学校等">
  </van-field>
  <van-field v-model="site" center clearable size="10px" placeholder="填写详细送餐地址">
  </van-field>
  <van-field v-model="ip" center clearable size="10px" placeholder="请填写可以联系到你的手机号码">
  </van-field>
  <van-field v-model="ip2" center clearable size="10px" placeholder="备用联系电话">
  </van-field>
  <van-button type="primary" block @click="add()">新增地址</van-button>
</div>
</template>

<script>
import Top from "@/components/Top";
import qs from "querystring";
export default {
  name: "Selectaddress",
  components: {Top},
  data() {
    return {
      name: "",
      school: "",
      site: "",
      ip: "",
      ip2: "",
      addsite3:""
    }
  },
  methods: {
    add() {
      let lok = JSON.parse(localStorage.user);
      this.axios.post('https://elm.cangdu.org/v1/users/'+lok.user_id+'/addresses', qs.stringify({
        user_id: lok.user_id,
        address: this.school,
        address_detail: this.site,
        geohash:this.addsite3.geohash,
        name: this.name,
        phone: this.ip,
        tag: "wu",
        sex: 1,
        phone_bk: this.ip2,
        tag_type: 3,
      })).then(data => {
        console.log(data.data);
        if (data.data.success=="添加地址成功"){
          this.$router.push("/myuser/shipping")
        }
      })
    },
    addsite() {
      this.$router.push("/myuser/shipping/addshipp/addsite")
    }
  },
  created() {
    this.$on("addsite2",(a)=>{
      this.addsite3=a;
      this.school=a.name;
      console.log(a)
    })
    let user=JSON.parse(localStorage.user)
    this.axios.get('https://elm.cangdu.org/v1/users/'+user.user_id+'/addresses').then(data=>{
      console.log(data.data)
    })
  }
}
</script>

<style scoped>
#root{
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  background-color: #f5f5f5;
}
</style>